package com.ruoyi.inspection.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.inspection.domain.GovernHiddenInventory;
import com.ruoyi.inspection.service.GovernHiddenInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专项检查隐患信息表(GovernHiddenInventory)表控制层
 *
 * @author makejava
 * @since 2024-07-30 11:37:39
 */
@RestController
@Api("隐患")
@RequestMapping("/check/governHiddenInventory")
public class GovernHiddenInventoryController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private GovernHiddenInventoryService governHiddenInventoryService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询
     *
     * @param governHiddenInventory 筛选条件
     * @return 查询结果
     */
    @ApiOperation("隐患列表")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:list')")
    @GetMapping("/list")
    public TableDataInfo queryByPage(GovernHiddenInventory governHiddenInventory) {
        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
        if (getUserId()!=1 && !zxgly) {
            governHiddenInventory.setRegisterUserId(getUserId().intValue());
        }

        startPage();
        governHiddenInventory.setGovernDeleted(0);
        List<GovernHiddenInventory> governHiddenInventories = this.governHiddenInventoryService.queryByPage(governHiddenInventory);
        return getDataTable(governHiddenInventories);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param hiddenInventoryId 隐患主键
     * @return 单条数据
     */
    @ApiOperation("查看隐患")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:query')")
    @GetMapping("{hiddenInventoryId}")
    public AjaxResult queryById(@PathVariable("hiddenInventoryId") Integer hiddenInventoryId) {
        return AjaxResult.success(this.governHiddenInventoryService.queryById(hiddenInventoryId));
    }

    /**
     * 新增数据
     *
     * @param governHiddenInventory 实体
     * @return 新增结果
     */
    @ApiOperation("隐患录入")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:add')")
    @PostMapping
    public AjaxResult add(@RequestBody GovernHiddenInventory governHiddenInventory) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governHiddenInventory.setGovernCreateBy(user.getUserId());
        governHiddenInventory.setGovernCreateBy(getUsername());
        governHiddenInventory.setHiddenCheckUser(getUsername());
        governHiddenInventory.setRegisterUserId(getUserId().intValue());
        governHiddenInventory.setRegisterUserName(getUsername());
        return AjaxResult.success(this.governHiddenInventoryService.insert(governHiddenInventory));
    }

    /**
     * 确认隐患
     * @return
     */
    @ApiOperation("隐患确认")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:edit')")
    @PostMapping("/confirm")
    public AjaxResult confirm(HttpServletRequest request, @RequestBody GovernHiddenInventory governHiddenInventory) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governHiddenInventory.setGovernUpdateBy(user.getUsername());
//        Long userId = user.getUserId();
        governHiddenInventoryService.confirm(governHiddenInventory, getLoginUser());

        return AjaxResult.success();
    }

    /**
     * 整改隐患
     * @return
     */
    @ApiOperation("隐患整改")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:edit')")
    @PostMapping("/repair")
    public AjaxResult repair(@RequestBody GovernHiddenInventory governHiddenInventory) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governHiddenInventory.setGovernUpdateBy(user.getUsername());
//        Long userId = user.getUserId();
        governHiddenInventoryService.repair(governHiddenInventory, getLoginUser());
        return AjaxResult.success();
    }

    /**
     * 复核隐患
     * @return
     */
    @ApiOperation("隐患复核")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:edit')")
    @PostMapping("/review")
    public AjaxResult review(@RequestBody GovernHiddenInventory governHiddenInventory) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governHiddenInventory.setGovernUpdateBy(user.getUsername());
//        Long userId = user.getUserId();
        governHiddenInventoryService.review(governHiddenInventory, getLoginUser());
        return AjaxResult.success();
    }

    /**
     * 上传隐患
     * @return
     */
    @ApiOperation("隐患上报")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:edit')")
    @GetMapping("/upload")
    public AjaxResult upload(Long[] hiddenInventoryIds) {
        governHiddenInventoryService.upload(hiddenInventoryIds);
        return AjaxResult.success();
    }

    /**
     * 编辑数据
     *
     * @param governHiddenInventory 实体
     * @return 编辑结果
     */
    @ApiOperation("隐患编辑")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody GovernHiddenInventory governHiddenInventory) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governHiddenInventory.setGovernUpdateBy(user.getUserName);
        governHiddenInventory.setGovernUpdateBy(getUsername());
        return AjaxResult.success(this.governHiddenInventoryService.update(governHiddenInventory));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("隐患删除")
    @PreAuthorize("@ss.hasPermi('check:governHiddenInventory:remove')")
    @DeleteMapping
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.governHiddenInventoryService.deleteById(id));
    }

}

